package com.sg.business.management.handler;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.mobnut.commons.util.Utils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.sg.business.resource.nls.Messages;
import com.sg.widgets.part.editor.fields.AbstractFieldPart;
import com.sg.widgets.part.editor.fields.value.IAddTableItemHandler;

public class AddStandardWorksOption implements IAddTableItemHandler {

	public class NameValueDialog extends Dialog {

		private Text nameText;
		private Text valueText;
		private BasicDBObject data;

		public NameValueDialog(Shell shell) {
			super(shell);
		}
		
		@Override
		protected Control createDialogArea(Composite parent) {
			 Composite composite = (Composite) super.createDialogArea(parent);
			 //add controls to composite as necessary
			 nameText = new Text(composite, SWT.BORDER);
			 nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			 valueText = new Text(composite, SWT.BORDER);
			 valueText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			 nameText.setMessage(Messages.get().StandardWorksOptionName);
			 valueText.setMessage(Messages.get().StandardWorksOptionValue);
			 return composite;
		}
		
		@Override
		protected void okPressed() {
			String value = valueText.getText();
			
			if(Utils.isNumbers(value)) {
				Double doublevalue = Double.valueOf(value);
				data = new BasicDBObject().append("name", nameText.getText()).append("value", doublevalue);
			}
			super.okPressed();
		}
		

	}

	public AddStandardWorksOption() {
	}

	@Override
	public boolean addItem(BasicDBList inputData, AbstractFieldPart part) {
		NameValueDialog dialog = new NameValueDialog(part.getShell());
		int ret = dialog.open();
		if(ret == Dialog.OK){
			if(dialog.data != null) {
				if(!inputData.contains(dialog.data)) {
			inputData.add(dialog.data);
				}
			return true;
			}
		}
		return false;
	}


}
